package app.qrcode.browser.chat.activities

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import app.qrcode.browser.chat.R
import app.qrcode.browser.chat.adapter.Contact
import app.qrcode.browser.chat.adapter.InviteInfo
import app.qrcode.browser.chat.adapter.InviteInfoAdapter
import app.qrcode.browser.chat.model.Model
import app.qrcode.browser.chat.model.bean.InvitationInfo
import com.hyphenate.chat.EMClient
import kotlinx.android.synthetic.main.activity_ask_chat.*

class AskChatActivity : AppCompatActivity() {

    private val inviteInfoAdapter by lazy { InviteInfoAdapter() }

    private var inviteList = ArrayList<InviteInfo>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ask_chat)
        initView()
        initRecyclerView()
    }

    private fun initView() {
        ask_chat_btn.setOnClickListener {
//            val intent = Intent(this, ChatActivity::class.java)
            val pUserName = edit_pusername_text.text.toString()
            Log.w("cdm", "AskChatActivity.initView -> 发送了一条好友请求")
            EMClient.getInstance().contactManager().addContact(pUserName, "来一发啊")
//            intent.putExtra("pName_data", pUserName)
//            startActivity(intent)
        }
    }

    private fun initRecyclerView() {
        val inviteLayoutManager = LinearLayoutManager(this)
        inviteRecyclerView.layoutManager = inviteLayoutManager
        inviteRecyclerView.adapter = inviteInfoAdapter
    }

    private fun getInviteInfo() {
        val invitationInfoList = Model.dbManager.inviteTableDao.getInvitations()
        invitationInfoList.forEach {
            if (it.status == InvitationInfo.InvitationStatus.INVITE_ACCEPT) {
                inviteList.add(
                    InviteInfo(
                        it.user?.hxId,
                        it.user?.photo,
                        it.reason,
                        Contact.ITEM_NOHEAD,
                        InviteInfo.AGREE_STATUS
                    )
                )
            } else
                inviteList.add(
                    InviteInfo(
                        it.user?.hxId,
                        it.user?.photo,
                        it.reason,
                        Contact.ITEM_NOHEAD,
                        InviteInfo.UNAGREE_STATUS
                    )
                )
        }
        inviteInfoAdapter.updateList(inviteList)
        Log.w("cdm", "AskChatActivity.getInviteInfo -> $inviteList")
    }

    override fun onResume() {
        super.onResume()
        getInviteInfo()
    }
}